/*
 * Copyright 2019 Flipkart Internet Pvt. Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.ohos.proteus.recyclerview.v7.adapter;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.app.AbilityContext;

import com.flipkart.ohos.proteus.DataContext;
import com.flipkart.ohos.proteus.ProteusContext;
import com.flipkart.ohos.proteus.ProteusLayoutInflater;
import com.flipkart.ohos.proteus.ProteusView;
import com.flipkart.ohos.proteus.value.Layout;
import com.flipkart.ohos.proteus.value.ObjectValue;
import com.flipkart.ohos.proteus.value.Value;

import java.util.Map;

/**
 * SimpleListProvider.
 *
 * @author adityasharat
 */
public class SimpleListProvider extends ProteusListContainerProvider<ProteusViewHolder> {
    private static final String ATTRIBUTE_ITEM_LAYOUT = "item-layout";
    private static final String ATTRIBUTE_ITEM_COUNT = "item-count";

    public static final Builder<SimpleListProvider> BUILDER = (view, config, abilityContext) -> {
        Layout layout = config.getAsObject().getAsLayout(ATTRIBUTE_ITEM_LAYOUT);
        Integer count = config.getAsObject().getAsInteger(ATTRIBUTE_ITEM_COUNT);
        ObjectValue data = view.getViewManager().getDataContext().getData();
        ProteusContext context = (ProteusContext) view.getContext();
        return new SimpleListProvider(context.getInflater(), data, layout, count != null ? count : 0, abilityContext);
    };

    private final ProteusLayoutInflater inflater;

    private final ObjectValue data;
    private final int count;
    private final Layout layout;
    private final Map<String, Value> scope;
    private final AbilityContext abilityContext;

    private SimpleListProvider(ProteusLayoutInflater inflater, ObjectValue data, Layout layout,
                               int count, AbilityContext abilityContext) {
        this.inflater = inflater;
        this.abilityContext = abilityContext;
        this.data = data;
        this.count = count;
        this.layout = new Layout(layout.type, layout.attributes, null, layout.extras);
        this.scope = layout.data;
    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        ProteusView view = inflater.inflate(layout, new ObjectValue());
        DataContext context = DataContext.create(view.getViewManager().getContext(), data, position, scope);
        view.getViewManager().update(context.getData());
        return (Component) view;
    }
}
